﻿using Helper;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using HC.CainiaoPostStation.DAL.Base;
using HC.CainiaoPostStation.Models;
using HC.CainiaoPostStation.Models.UIModels;
using HC.CaiNiaoPostStation.Models;

namespace HC.CaiNiaoPostStation.DAL
{
    public class ShelfDAL:BaseDAL<ShelfInfo>
    {
        /// <summary>
        /// 分页查询货架信息列表
        /// </summary>
        /// <param name="keywords"></param>
        /// <param name="isDeleted"></param>
        /// <param name="startIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public PageModel<ShelfInfo> FindShelvesList(string keywords, int isDeleted, int startIndex, int pageSize)
        {
            string strWhere = $"IsDeleted={isDeleted}";
            if(!string.IsNullOrEmpty(keywords))
            {
                strWhere += " and (ShelfNo like @keywords or ShelfName  like @keywords  or Address  like @keywords or Remark  like @keywords )";
            }
            SqlParameter para = new SqlParameter("@keywords", $"%{keywords}%");
            string cols = CreateSql.GetColNames<ShelfInfo>("");
            return GetRowsModelList<ShelfInfo>(strWhere, cols, "Id", "ShelfId", startIndex, pageSize, para);
        }

        /// <summary>
        /// 获取绑定下拉框的货架列表
        /// </summary>
        /// <param name="stationId"></param>
        /// <returns></returns>
        public List<ShelfInfo> GetCboShelveList(int stationId)
        {
            string strWhere = "IsDeleted=0";
            if (stationId > 0)
                strWhere += " and StationId=" + stationId;
            return GetModelList(strWhere, "ShelfId,ShelfName", "");
        }

        /// <summary>
        /// 添加货架信息
        /// </summary>
        /// <param name="shelf"></param>
        /// <returns></returns>
        public int AddShelf(ShelfInfo shelf)
        {
            string cols = CreateSql.GetColNames<ShelfInfo>("ShelfId");
            return Add(shelf, cols, 1);
        }

        /// <summary>
        /// 检查编码或名称是否已存在
        /// </summary>
        /// <param name="value"></param>
        /// <param name="isNo"></param>
        /// <param name="stationId"></param>
        /// <returns></returns>
       public  bool ExistShelfNoOrName(string value, bool isNo, int stationId = 0)
        {
            if (isNo)
            {
                return ExistsByName("ShelfNo", value);
            }
            else
            {
                if (stationId == 0)
                    return ExistsByName("ShelfName", value);
                else
                    return ExistsByName("ShelfName", value, "StationId", stationId);
            }
                
        }

        /// <summary>
        /// 删除货架(可多个)
        /// </summary>
        /// <param name="shelfIds"></param>
        /// <param name="delType"></param>
        /// <param name="isDeleted"></param>
        /// <returns></returns>
        public bool UpdateShelfDelStates(List<int> shelfIds, int delType, int isDeleted)
        {
            string strIds = string.Join(",", shelfIds);
            string strWhere = $"ShelfId in ({strIds})";
            List<string> sqlList = new List<string>();
            if (delType == 0)
            {
                sqlList.Add(CreateSql.CreateLogicDeleteSql<ShelfInfo>(strWhere, isDeleted));
            }
            else
            {
                sqlList.Add(CreateSql.CreateDeleteSql<ShelfInfo>(strWhere));
            }
            return SqlHelper.ExecuteTrans(sqlList);
        }

    }
}
